package com.chaychan.bottombarlayout_ohos.slice;

import com.chaychan.bottombarlayout_ohos.ResourceTable;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DynamicAddItemActivity extends AbilitySlice {


    private List<TabFragment> mFragmentList = new ArrayList<>();
    private BottomBarLayout mBottomBarLayout;
    private AbilitySlice abilitySlice;

    private int[] mNormalIconIds = new int[]{
            ResourceTable.Media_tab_home_normal, ResourceTable.Media_tab_video_normal,
            ResourceTable.Media_tab_micro_normal, ResourceTable.Media_tab_me_normal
    };

    private int[] mSelectedIconIds = new int[]{
            ResourceTable.Media_tab_home_selected, ResourceTable.Media_tab_video_selected,
            ResourceTable.Media_tab_micro_selected, ResourceTable.Media_tab_me_selected
    };

    private int[] mTitleIds = new int[]{
            ResourceTable.String_tab_home,
            ResourceTable.String_tab_video,
            ResourceTable.String_tab_micro,
            ResourceTable.String_tab_me
    };


    public DynamicAddItemActivity(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;
    }


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dynamic_add_item);

        initView();
        initData();
        initListener();

    }

    private void initView() {
        mBottomBarLayout = (BottomBarLayout) findComponentById(ResourceTable.Id_bbl);

        Image image = (Image) findComponentById(ResourceTable.Id_menuImg);
        image.setClickedListener(component -> {
            MenuAddDialog menuDialog = new MenuAddDialog(getContext());
            menuDialog.setonMenuSelectedListener(new MenuAddDialog.OnMenuSelectedListener() {
                @Override
                public void onMenuSelected(int position) {

                    switch (position) {
                        case 0:
                            int random = new Random().nextInt(3);
                            //addFragment
                            mFragmentList.add(createFragment(mTitleIds[random]));
                            //addItem
                            BottomBarItem bottomBarItem = createBottomBarItem(random);
                            mBottomBarLayout.addItem(bottomBarItem);

                            mBottomBarLayout.setCurrentItem(mFragmentList.size() - 1);
                            break;
                        case 1:
                            //移除条目
                            mBottomBarLayout.removeItem(0);

                            if (mFragmentList.size() != 0) {
                                mFragmentList.remove(0);

                                if (mFragmentList.size() != 0) {
                                    mBottomBarLayout.setCurrentItem(0);
                                }
                            }
                            break;
                    }
                }
            });
            menuDialog.show();
        });
    }

    private void initData() {
        mBottomBarLayout.removeAllComponents();
        for (int i = 0; i < mTitleIds.length; i++) {
            //创建item
            BottomBarItem item = createBottomBarItem(i);
            mBottomBarLayout.addItem(item);

            TabFragment homeFragment = createFragment(mTitleIds[i]);
            mFragmentList.add(homeFragment);
        }
        changeFragment(0); //默认显示第一页
    }

    private TabFragment createFragment(int titleId) {
        return new TabFragment(abilitySlice, getString(titleId));
    }


    private BottomBarItem createBottomBarItem(int i) {
        BottomBarItem item = new BottomBarItem.Builder(this)
                .titleTextBold(true)
                .titleTextSize(8)
                .titleNormalColor(ResourceTable.Color_tab_normal_color)
                .titleSelectedColor(ResourceTable.Color_tab_selected_color)
                //              .openTouchBg(false)
                //              .marginTop(5)
                //              .itemPadding(5)
                //              .unreadNumThreshold(99)
                //              .unreadTextColor(R.color.white)

                //还有很多属性，详情请查看Builder里面的方法
                //There are still many properties, please see the methods in the Builder for details.
                .create(mNormalIconIds[i], mSelectedIconIds[i], getString(mTitleIds[i]));
        return item;
    }


    private void initListener() {
        mBottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                changeFragment(currentPosition);
            }
        });
    }


    private void changeFragment(int currentPosition) {
        ((FractionAbility) getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_stackLayout, mFragmentList.get(currentPosition))
                .submit();
    }

}
